/*
Problem Description
Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
 

Input
One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
 

Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
 

Sample Input
2 1
1 2
2 2
1 2
2 1
 

Sample Output
1777
-1
--------------------------------------------------------------
题意：多个测试数据，每个测试数据第一行包含两个数n、m，代表有n个人，每个人最少发的奖金是888，现有m个要求，
有m行a和b，表示a要求比b的奖金多。
问能否满足所有的要求，如果能，则n个人得到的奖金总和最少是多少，不能满足所有要求就输出-1。
 */
package com.yuan.algorithms.training20150807;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author YouYuan
 * @eMail E-mail:1265161633@qq.com
 * @Time 创建时间：2015年8月11日 下午4:36:35
 * @Explain 说明:使用邻接表构图，节省内存开销
 */
public class Hdu2647逆向拓扑_发奖金 {

	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	static int nextInt() throws IOException { in.nextToken(); return (int)in.nval;}
	static List<ArrayList<Integer>> arc = new ArrayList<ArrayList<Integer>>();//存储图的邻接表
	static int[] degree;//存储入度
	static int n, m;
	public static void main(String[] args) throws IOException {
		while(in.nextToken() != in.TT_EOF) {
			n = (int) in.nval;
			m = nextInt();
			initialise();
			structure();
			topologySort();
		}
	}
	
	/**
	 * 拓扑排序
	 */
	private static void topologySort() {
		int[] salary = new int[n+1];//工资需要增加的数量
		Queue<Integer> queue = new LinkedList<Integer>();
		int sum = 0;//入度为0的总数
		int pay = 0;//增加的工资总数
		for (int i = 1; i <= n; i++) {
			if (degree[i] == 0) {
				queue.add(i);//所有度为0的顶点入队
			}
		}
		while(!queue.isEmpty()) {
			int index = queue.poll();//入度为0的下标，即b的下标
			sum++;
			for(int i : arc.get(index)) {
				degree[i]--;//以b为前驱的所有顶点入度-1
				if (degree[i] == 0) {
					queue.add(i);
					salary[i] = salary[index] + 1;//a的工资=b的工资+1
					pay += salary[i];//记录需要添加的工资总数
				}
			}
		}
		//sum!=n代表图中还有数据未处理（有环），无解
		out.println(sum != n ? -1 : (pay + n * 888));
		out.flush();
	}

	/**
	 * 初始化
	 */
	private static void initialise() {
		arc.clear();
		for (int i = 0; i <= n; i++) {
			arc.add(new ArrayList<Integer>());
		}
		degree = new int[n+1];
	}
	
	/**
	 * 构建图
	 * @throws IOException
	 */
	private static void structure() throws IOException {
		while(--m>=0) {
			int a = nextInt();
			int b = nextInt();
			//a要求工资比b多，b指向a，所以建图时反过来
			if (!arc.get(b).contains(a)) {//去除重复数据
				arc.get(b).add(a);
				degree[a]++;
			}
		}
	}

}
